package com.sgcc;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class Login extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /**
         * 先获取用户输入的内容（request对象）
         * 判断用户名和密码是否都是admin，如果不是，重定向到登陆界面，如果是，则登陆成功
         */
        String username = request.getParameter("username");
        String password=request.getParameter("password");
        if("admin".equals(username) && "admin".equals(password)){
            /*  	 设置response缓冲编码
             * 		response.setCharacterEncoding("UTF-8");
             *  * 设置浏览器打开文件所采用的编码
             *  	response.setHeader("Content-Type", "text/html;charset=UTF-8");
             *  * 简写方式
             *  	response.setContentType("text/html;charset=UTF-8");
             *  */
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().write("登录");

        }else{

			/*重定向
			response.setStatus(302);
			response.setHeader("location", "/day10/html/login.html");
			//response对象提供了一个方法，可以完成重定向。
			response.sendRedirect("/day10/response/login.html");*/
            //转发
            request.setAttribute("msg", "用户名或密码错误！！！");
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
